package com.bawei.a2010douyin.ui.push;

import com.bawei.a2010douyin.base.mvp.BasePresenter;
import com.bawei.a2010douyin.base.rx.BaseFunc;
import com.bawei.a2010douyin.entity.PushEntity;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class PushPresenter extends BasePresenter<PushContract.IPushView> implements PushContract.IPushPresenter {
    private PushContract.IPushModel mIPushModel;
    public PushPresenter() {
        mIPushModel = new PushModel();
    }

    @Override
    public void startPush(RequestBody requestBody) {
        mIPushModel.startPush(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new BaseFunc<PushEntity>())
                .subscribe(new Observer<PushEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(PushEntity pushEntity) {
                        if(isAttach()){
                            getView().showStartPush(pushEntity);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void stopPush() {
        mIPushModel.stopPush()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new BaseFunc<String>())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(String data) {
                        if(isAttach()){
                            getView().showStopPush(data);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
